package com.sdkj.controller.law;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sdkj.base.common.FileDownload;
import com.sdkj.base.common.FileUpload;
import com.sdkj.controller.base.BaseController;
import com.sdkj.entity.Page;
import com.sdkj.service.law.LawRegulationService;
import com.sdkj.util.Const;
import com.sdkj.util.DateUtil;
import com.sdkj.util.Jurisdiction;
import com.sdkj.util.PageData;
import com.sdkj.util.PathUtil;

@Controller
@RequestMapping(value="/lawregulation")
public class LawRegulationController extends BaseController{
	
	String menuUrl = "lawregulation/list.do"; //菜单地址(权限用)
	@Resource(name="lawRegulationService")
	private LawRegulationService lawRegulationService;
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表");
		//if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	varList = lawRegulationService.list(page);	//列出LawMr列表
			mv.setViewName("law/lawRegulation/lawRegulation_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("law/lawRegulation/lawRegulation_add");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/*
	 * 保存
	 */
	@RequestMapping(value="/save")
	public ModelAndView save(MultipartFile file,HttpServletRequest request)throws Exception{
		logBefore(logger, "新增lawre数据");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData(request);
		String time = DateUtil.getDay();
		String filePath = null;
		String fileName = null;
			if (null != file && !file.isEmpty()) {
				filePath =PathUtil.getClasspath()+ Const.FILEPATHFILE+time;
				fileName = FileUpload.fileUpload(file, filePath, this.get32UUID());	
			}else {
				System.out.println("上传失败");
			}
		pd.put("regulation_id", this.get32UUID());
		pd.put("user_id", getCurrentUser().getUSER_ID());
		pd.put("regulation_person",getCurrentUser().getUSERNAME());
		pd.put("regulation_status", 0);
		pd.put("dept_id", getCurrentUser().getDepartmentId());
		pd.put("company_id", getCurrentUser().getCompanyId());
		pd.put("regulation_attachment",filePath);
		pd.put("regulation_name", fileName);
		lawRegulationService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 去往修改页面
	 */
	@RequestMapping(value="goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改LawReceive页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = lawRegulationService.findById(pd);	//根据ID读取
			mv.setViewName("law/lawRegulation/lawRegulation_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
		logBefore(logger, "修改LawMr");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		lawRegulationService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/*
	 * 下载功能
	 */
	@RequestMapping(value="download")
	public void download(HttpServletResponse response)throws Exception{
		logBefore(logger, "下载功能");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {return;}
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = lawRegulationService.findById(pd);
		String filePath = (String) pd.get("regulation_attachment");
		String fileName = (String) pd.get("regulation_name");
		FileDownload.fileDownload(response, filePath+"/"+fileName, fileName);
	}
	
	
	
	
	
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}	
	
	
	
	
	
	
	
	
}
